package com.ty.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.ty.dto.Order;
import com.ty.dto.OrderQuery;
import com.ty.dto.OrderingDto;
import com.ty.dto.OrderingOk;
import com.ty.service.IOderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/order")
@Slf4j
public class OrderingController {
    @GetMapping
    @SentinelResource(value = "ordering", blockHandler = "ordering_blockHandler")
    public String ordering(@RequestParam Integer userId) {
        log.debug("正常业务请求进来了");
        return "下单成功";
    }
    public static String ordering_blockHandler(Integer userId, BlockException exception) {
        log.debug("限流请求进来了");
        return "操作频繁，请稍后再试";
    }

    // 限流配置规则
    @PostConstruct
    public static void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule(); //创建限流规则
        rule.setResource("ordering"); //设置资源名称
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);//QPS 每秒的访问量
        // Set limit QPS to 20.
        rule.setCount(2);

        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }


   /* @RequestMapping
    public OrderingOk ordering(@RequestBody OrderingDto dto){
        System.out.println(dto);

        OrderingOk ok = new OrderingOk().setId(1L).setNeedPay(BigDecimal.TEN);
        return ok;
    }

    @GetMapping
    public List<Order> ordering(OrderQuery query) {

        List<Order>  orders = new ArrayList<>();
        for (int i = 1; i <= 2; i++){
            orders.add(new Order().setId(Long.valueOf(i)).setUserId(query.getUserId())
                    .setTotalPrice(BigDecimal.TEN));
        }

        return orders;
    }

    @GetMapping("/sleep")
    public String sleep (@RequestParam Integer time,HttpServletRequest request) {
        String token = request.getHeader("token");

      *//*  String traceId = request.getHeader("traceId");
        System.out.println("traceId:"+traceId);

      ServletRequestAttributes requestAttributes =  (ServletRequestAttributes)(RequestContextHolder.getRequestAttributes());
        String traceId2 = requestAttributes.getRequest().getHeader("traceId");
        System.out.println("traceId2:"+traceId2);

        System.out.println(traceId);*//*
        log.debug("请求进来了,token:{}",token);
        ThreadUtil.safeSleep( time*1000);
        return "cold";
    }*/

}
